package com.jbh.wanandroidkotlin.manager

import android.app.Activity

/**
 *
 * 饿汉式  实现单例模式
 *
 */
class AppActivityManager {


    /**
     *
     *   记录 活动
     *
     */
    private val list = linkedMapOf<String,Activity>()






    companion object{
        val instance : AppActivityManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){
            AppActivityManager()
        }
    }



    /**
     * 添加
     *
     */
    fun add(activity: Activity){
        println("simpleName = ${activity::class.java.simpleName}")
        list[activity::class.java.simpleName] = activity
    }





    /**
     * 删除
     */
    fun remove(activity: Activity){
        list.remove(activity::class.java.simpleName)
    }





    /**
     * 是否包含
     */
    fun isContain(activityName: String?):Boolean{
        list?.let {
            return it.contains(activityName)
        }
    }




    /**
     * 集合  删除技巧
     *
     */
    fun exit(activityName: String? = null){

        val  it = list?.iterator()

        while (it.hasNext()){

            val result = it.next()

            if (activityName != result.key ){

                result.value.finish()

                it.remove()
            }
        }
    }
}
